package com.jic.market.impl.rest.discountamount;

import com.jic.market.common.exception.MarketErrorCodeEnum;
import com.jic.market.common.exception.MarketException;
import com.jic.market.common.util.Money;
import com.jic.market.entity.GoodsSku;
import com.jic.market.entity.PatchGrouponThemeEntity;
import com.jic.market.enums.FrontPromotionTypeEnum;
import com.jic.market.impl.rest.discountamount.po.GoodsPo;
import com.jic.market.impl.rest.discountamount.po.PromotionDataPo;
import com.jic.market.impl.rest.discountamount.po.PromotionPo;
import com.jic.market.mapper.PatchGrouponThemeEntityMapper;
import com.jic.market.mapper.PromotionEntityMapper;
import com.jic.market.mapper.base.BaseActivityMapper;
import com.jic.market.request.rest.discount.PromotionGoods;
import com.jic.market.service.rest.discountamount.PromotionGoodsService;
import com.jic.product.after.api.ProductListApi;
import com.jic.product.after.request.GoodsPageQueryRequest;
import com.jic.product.after.response.ProductStoreInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yinhaijun
 * @date: 2020/4/9
 */
@Service
public class PromotionGoodsServiceImpl implements PromotionGoodsService {

    @Autowired
    private PromotionEntityMapper promotionEntityMapper;

    @Resource
    private ProductListApi productListApi;

    @Autowired
    private BaseActivityMapper baseActivityMapper;
    @Autowired
    private PatchGrouponThemeEntityMapper patchGrouponThemeEntityMapper;
    @Override
    public Map<Long, GoodsPo> buildGoodsPoMap(List<PromotionGoods> promotionGoodsList){
        Map<Long, GoodsPo> map = new HashMap<>();
        for(PromotionGoods promotionGoods : promotionGoodsList){
            GoodsPo goodsPo = buildgoodsPo(promotionGoods);
            map.put(goodsPo.getSkuId(), goodsPo);
        }
        return map;
    }

    @Override
    public Map<Long, List<PromotionPo>> buildPromotionPoMap(Map<Long, GoodsPo> goodsPoMap, List<String> couponCodeList, String grouponPromotionCode, FrontPromotionTypeEnum frontPromotionTypeEnum){
        Map<Long, List<PromotionPo>> skuPromotionMap = new HashMap<>();
        goodsPoMap.forEach((skuId, goodsPo) ->{
            List<PromotionPo> skuPromotionList = new ArrayList<>();
            // 通用活动
            List<PromotionPo> promotionPoList = promotionEntityMapper.queryPromotionBySkuId(skuId);
            skuPromotionList.addAll(promotionPoList);
            // 优惠卷
            if(null != couponCodeList && !couponCodeList.isEmpty()){
                List<PromotionDataPo> couponPromotionDataPoList = baseActivityMapper.queryCouponPromotionBySkuIdAndCouponCodes(skuId, couponCodeList);
                if(null != couponPromotionDataPoList && !couponPromotionDataPoList.isEmpty()){
                    List<PromotionPo> couponPromotionPoList = new ArrayList<>();
                    for(PromotionDataPo promotionDataPo: couponPromotionDataPoList){
                        PromotionPo promotionPo = getOldPromotion(couponPromotionPoList, promotionDataPo.getPromotionId());
                        if(null != promotionPo){
                            promotionPo.getCouponCodeList().add(promotionDataPo.getCouponCode());
                        }else {
                            PromotionPo newPromotionPo = new PromotionPo();
                            newPromotionPo.setPromotionId(promotionDataPo.getPromotionId());
                            newPromotionPo.setPromotionCode(promotionDataPo.getPromotionId());
                            newPromotionPo.setFrontPromotionType(promotionDataPo.getFrontPromotionType());
                            List<String> couponCodeList11 = new ArrayList<>();
                            couponCodeList11.add(promotionDataPo.getCouponCode());
                            newPromotionPo.setCouponCodeList(couponCodeList11);
                            changeFrontPromotionType(newPromotionPo);
                            couponPromotionPoList.add(newPromotionPo);
                        }
                    }
                    skuPromotionList.addAll(couponPromotionPoList);
                }
            }
            //拼团
            if(null != grouponPromotionCode && !"".equals(grouponPromotionCode)){
                PromotionPo promotionPo = new PromotionPo();
                promotionPo.setPromotionCode(grouponPromotionCode);
                PatchGrouponThemeEntity patchGrouponThemeEntityQ = new PatchGrouponThemeEntity();
                patchGrouponThemeEntityQ.setDeleteFlag(0);
                patchGrouponThemeEntityQ.setGroupCode(grouponPromotionCode);
                PatchGrouponThemeEntity patchGrouponThemeEntity = patchGrouponThemeEntityMapper.query(patchGrouponThemeEntityQ);
                if(null == patchGrouponThemeEntity){
                    throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, "未查询到此拼团活动" + grouponPromotionCode);
                }
                promotionPo.setPromotionId(patchGrouponThemeEntity.getId().toString());
                promotionPo.setFrontPromotionType(FrontPromotionTypeEnum.GROUPON.getCode());
                skuPromotionList.add(promotionPo);
            }

            // TODO 其它商品表也需要查询一遍


            // 测试单个活动,需要移除其他
            if(null != skuPromotionList && !skuPromotionList.isEmpty() && null != frontPromotionTypeEnum){
                for(int i = skuPromotionList.size()-1;i>=0;i--){
                    if(!skuPromotionList.get(i).getFrontPromotionType().equals(frontPromotionTypeEnum.getCode())){
                        skuPromotionList.remove(i);
                    }
                }
            };

            // 放入一起
            if(null != skuPromotionList && !skuPromotionList.isEmpty()){
                skuPromotionMap.put(skuId, skuPromotionList);
            }


        });
        return skuPromotionMap;
    }

    private PromotionPo getOldPromotion(List<PromotionPo> couponPromotionPoList, String promotionId) {
        for(PromotionPo promotionPo : couponPromotionPoList){
            if(promotionPo.getPromotionId().equals(promotionId)){
                return promotionPo;
            }
        }
        return null;
    }


    /** 优惠劵枚举转化 */
    private void changeFrontPromotionType(PromotionPo promotionPo) {
        if(promotionPo.getFrontPromotionType().equals(1100)){
            promotionPo.setFrontPromotionType(FrontPromotionTypeEnum.COUPON_.getCode());
        }
        if(promotionPo.getFrontPromotionType().equals(1200)){
            promotionPo.setFrontPromotionType(FrontPromotionTypeEnum.RED.getCode());
        }
    }

    private GoodsPo buildgoodsPo(PromotionGoods promotionGoods) {
//        GoodsSku goodsSkuQ = new GoodsSku();
//        goodsSkuQ.setThirdSkuId(promotionGoods.getThirdProductCode());
//        List<GoodsSku> goodsSkuList = goodsSkuMapper.query(goodsSkuQ);
        List<ProductStoreInfo> goodsSkuList = productListApi.queryProductBySpuList(new GoodsPageQueryRequest() {{
            setGoodsCode(promotionGoods.getThirdProductCode());
        }});
        if(null == goodsSkuList || goodsSkuList.isEmpty()){
            throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, promotionGoods.getThirdProductCode() + "商品不存在");
        }
        if(goodsSkuList.size() >= 2){
            throw new MarketException(MarketErrorCodeEnum.PARAM_ERROR, promotionGoods.getThirdProductCode() + "该商品数据库有多个");
        }
        ProductStoreInfo goodsSku = goodsSkuList.get(0);
        GoodsPo goodsPo = new GoodsPo();
        goodsPo.setSkuId(goodsSku.getId());
        goodsPo.setNum(promotionGoods.getNum());
        goodsPo.setMarketPrice(goodsSku.getSalePriceWithTax());
        goodsPo.setThirdSkuId(promotionGoods.getThirdProductCode());
        goodsPo.setTotalAmount(Money.multiply(goodsSku.getSalePriceWithTax(),new BigDecimal(promotionGoods.getNum())));
        return goodsPo;
    }
}
